import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/video.dart';
import 'package:qgchat/http/models/giftlistModel.dart';
import 'package:qgchat/states/states_model/index.dart';
import 'package:qgchat/widgets/gitfSvga.dart';
import 'package:qgchat/widgets/tipsDialog.dart';

class GiftBoxBloc implements BlocBase {
  // ##########  STREAMS  ##############

  /// 礼物列表
  StreamController _gift = StreamController<List<List<GiftlistModel>>>.broadcast();
  Sink get _giftSink => _gift.sink;
  Stream get giftStream => _gift.stream;

  /// 选择礼物
  StreamController _choice = StreamController<ChoiceGift>.broadcast();
  Sink get _choiceSink => _choice.sink;
  Stream get choiceStream => _choice.stream;

  /// 礼物总价格
  StreamController _giftPrice = StreamController<int>.broadcast();
  Sink get _giftPriceSink => _giftPrice.sink;
  Stream get giftPriceStream => _giftPrice.stream;

  /// 礼物数量
  StreamController _giftCount = StreamController<int>.broadcast();
  Sink get _giftCountSink => _giftCount.sink;
  Stream get giftCountStream => _giftCount.stream;

  // ##########  HANDLING  ##############

  /// 礼物列表
  List<List<GiftlistModel>> gitflist = [];

  /// 当前选择的礼物 group第几组 unit第几个
  ChoiceGift choice = ChoiceGift(group: 0, unit: 0);

  /// 礼物数量
  int giftCount = 1;

  /// 礼物总价
  int gitfPrice = 0;

  GiftBoxBloc() {
    getGitf();
  }

  void dispose() {
    _gift.close();
    _choice.close();
    _giftPrice.close();
    _giftCount.close();
  }

  /// 礼物列表
  Future getGitf() async {
    List<GiftlistModel> list = await VideoApis.giftlist();
    if (!_gift.isClosed) {
      List<List<GiftlistModel>> newlist = [];
      for (var i = 0; i < list.length; i += 8) {
        if (i + 8 > list.length) {
          newlist.add(list.sublist(i, list.length));
        } else {
          newlist.add(list.sublist(i, i + 8));
        }
      }

      gitflist = newlist;
      _giftSink.add(newlist);
      countPrice();
    }
  }

  /// 选择礼物
  void choiceGift(int group, int unit) {
    choice = ChoiceGift(group: group, unit: unit);
    _choiceSink.add(choice);

    giftCount = 1;
    _giftCountSink.add(giftCount);
    countPrice();
  }

  /// 改变礼物数量
  void choiceGiftCount(bool isAdd) {
    if (isAdd) {
      _giftCountSink.add(++giftCount);
      countPrice();
    } else if (giftCount > 1) {
      _giftCountSink.add(--giftCount);
      countPrice();
    }
  }

  /// 计算礼物总价格
  void countPrice() {
    int price = gitflist[choice.group][choice.unit].giftGold;
    gitfPrice = price * giftCount;
    _giftPriceSink.add(gitfPrice);
  }

  /// 送出礼物
  /// vid 为小视频中赠送礼物的视频id 小视频非直播
  void givingGifts(BuildContext context, String token, {int vid, bool canShowSvga}) async {
    UserState user = state(context, 'user');
    GiftlistModel gift = gitflist[choice.group][choice.unit];
    Map result;

    if (token == user.info.memberToken) {
      showToast("为什么你要送给自己鸭");
      return;
    }
    if (gitfPrice > user.info.gold) {
      TipsDialog.noGoldGivingGifts(context, () {
        goPage(context, '/recharge');
      });
      return;
    }

    if (vid == null) {
      result = await VideoApis.sendGift(gift.id, giftCount, token);
    } else {
      result = await VideoApis.gifVedio(gift.id, giftCount, token, vid);
    }

    if (canShowSvga) {
      GitfSvga.show(gift.id);
    }
    if (result['status'] != 1) {
      showToast("${result['msg']}");
    }
  }
}

/// 选择礼物模型
class ChoiceGift {
  ChoiceGift({this.group, this.unit});

  int group;
  int unit;
}